package com.example.kechengsheji;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class cangchuguanli extends AppCompatActivity {
    //按键
    private Button insertButton,updateButton,searchButton,deleteButton;
    //输入框
    private EditText bshangpin,bmoney,bnum;
    //纯文本
    private TextView showshangpin,shownum,showmoney;
    final DatabaseHelper dbHelper=new DatabaseHelper(cangchuguanli.this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cangchuguanli);

        insertButton=findViewById(R.id.btn_insert);
        updateButton=findViewById(R.id.btn_update);
        searchButton=findViewById(R.id.btn_search);
        deleteButton=findViewById(R.id.btn_delete);

        bshangpin=findViewById(R.id.bshangpin);
        bmoney=findViewById(R.id.bmoney);
        bnum=findViewById(R.id.bnum);

        showshangpin=findViewById(R.id.tv_showshangpin);
        shownum=findViewById(R.id.tv_shownum);
        showmoney=findViewById(R.id.tv_showmoney);

        SQLiteDatabase db=dbHelper.getReadableDatabase();

//        myShow();



        insertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("bshangpin",bshangpin.getText().toString());
                values.put("bmoney",bmoney.getText().toString());
                values.put("bnum",bnum.getText().toString());
                long id =db.insert("information",null,values);
                Log.d("myDeBug","insert");


                db.close();
                bshangpin.setText(null);
                bmoney.setText(null);
                bnum.setText(null);
            }
        });
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("bnum",bnum.getText().toString());
                values.put("bmoney",bmoney.getText().toString());
                db.update("information",values,"bshangpin=?",new String[]{bshangpin.getText().toString()});
                myShow();

                db.close();
                Log.d("myDebug","update");
                bshangpin.setText(null);
                bmoney.setText(null);
                bnum.setText(null);
            }
        });
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                String name1=bshangpin.getText().toString();
//                showshangpin.setText(null);
//                shownum.setText(null);
//                showmoney.setText(null);
                if(name1.equals("")){
                    showshangpin.setText("商品");
                    shownum.setText("数量");
                    showmoney.setText("单价");
                    Cursor cursor = db.rawQuery("select * from information",null);

                    while (cursor.moveToNext()) {
                       @SuppressLint("Range") String newshangpin = cursor.getString(cursor.getColumnIndex("bshangpin"));
                       @SuppressLint("Range") int newnum = cursor.getInt(cursor.getColumnIndex("bnum"));
                       @SuppressLint("Range") int newmoney = cursor.getInt(cursor.getColumnIndex("bmoney"));
                        showshangpin.setText(showshangpin.getText() + "\n" + newshangpin);
                        shownum.setText(shownum.getText()+"\n" + newnum);
                        showmoney.setText(showmoney.getText()+"\n" + newmoney);

                    }



                }else {

                    Cursor cursor = db.rawQuery("select * from information where bshangpin = ? ", new String[]{name1});
                    while (cursor.moveToNext()) {
                        @SuppressLint("Range") String newshangpin = cursor.getString(cursor.getColumnIndex("bshangpin"));
                        @SuppressLint("Range") int newnum = cursor.getInt(cursor.getColumnIndex("bnum"));
                        @SuppressLint("Range") int newmoney = cursor.getInt(cursor.getColumnIndex("bmoney"));

                        showshangpin.setText(showshangpin.getText() + "\n" + newshangpin);
                        shownum.setText(shownum.getText()+"\n" + newnum);
                        showmoney.setText(showmoney.getText()+"\n" + newmoney);
                        db.close();
                    }

                    cursor.close();

                    bshangpin.setText(null);
                    bmoney.setText(null);
                    bnum.setText(null);
                }
            }
        });
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                db.delete("information","bshangpin=?",new String[]{bshangpin.getText().toString()});
                myShow();
                db.close();
                Log.d("myDeBug","DeleteSuccess");
                bshangpin.setText(null);
                bnum.setText(null);
                bmoney.setText(null);
            }
        });

    }

    public void myShow(){
        SQLiteDatabase db=dbHelper.getReadableDatabase();

        showshangpin.setText("商品");
        showmoney.setText("单价");
        shownum.setText("数量");
        Cursor cursor = db.rawQuery("select * from information",null);

        while (cursor.moveToNext()) {
            @SuppressLint("Range") String newshangpin = cursor.getString(cursor.getColumnIndex("bshangpin"));
            @SuppressLint("Range") int newnum = cursor.getInt(cursor.getColumnIndex("bnum"));
            @SuppressLint("Range") int newmoney = cursor.getInt(cursor.getColumnIndex("bmoney"));
            showshangpin.setText(showshangpin.getText() + "\n" + newshangpin);
            shownum.setText(shownum.getText()+"\n" + newnum);
            showmoney.setText(showmoney.getText()+"\n" + newmoney);
        }
        cursor.close();
        db.close();
    }}
